package com.choosefine.devops.dms.base;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.choosefine.devops.dms.common.def.ResultCode;
import com.choosefine.devops.dms.common.json.JsonResult;


/**
 * 控制层的共性
 * 
 * @auth liaozhanggen 2016-11-19 上午9:42:30
 * 
 */
public class BaseController {

	private Logger logger = LoggerFactory.getLogger(BaseController.class);
	protected final static String DATE_FORMATE = "yyyy-MM-dd";
	private JsonResult result;

	/**
	 * @param data
	 *            返回的展示数据 json 对象
	 * @param MESSAGE
	 *            返回的成功或失败的原因 比如：SUCCESS / ERROR ....
	 * @param resultCode
	 *            返回的成功或失败的编码 比如：200、 500、400....
	 * 
	 * @auth liaozhanggen 2016-11-19 上午10:14:42
	 */
	public JsonResult getInstanceJsonResult(Object data, String MESSAGE,
			Integer resultCode) {
			logger.info("--------------getInstanceJsonResult--------------> new");
			result = new JsonResult(data, MESSAGE, resultCode);
		return result;
	}

	public JsonResult responseResult(Object data, String MESSAGE,
			Integer resultCode) {
			logger.info("--------------getInstanceJsonResult--------------> new");
			result = new JsonResult(data, MESSAGE, resultCode);
		return result;
	}

	public String responseCreateSuccess(Object data) {
		logger.info("后端返回对象：{}", data);
			result = new JsonResult(data, ResultCode.MSG_CREATE_SUCCESS,
					ResultCode.SUCCESS_CREATED);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseDeleteSuccess(Object data) {
		logger.info("后端返回对象：{}", data);
			result = new JsonResult(data, ResultCode.MSG_SUCCESS,
					ResultCode.SUCCESS);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseSuccess() {
			result = new JsonResult(ResultCode.MSG_SUCCESS, ResultCode.SUCCESS);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseSuccess(Object data) {
		logger.info("后端返回对象：{}", data);
			result = new JsonResult(data, ResultCode.MSG_SUCCESS,
					ResultCode.SUCCESS);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseSuccess(Object data, String MESSAGE) {
		logger.info("后端返回对象：{}", data);
			result = new JsonResult(data, MESSAGE, ResultCode.SUCCESS);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseSuccess(Object data, Integer resultCode,
			String MESSAGE) {
		logger.info("后端返回对象：{}", data);
			result = new JsonResult(data, MESSAGE, resultCode);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseArraySuccess(List<?> arraydata) {
		logger.info("后端返回对象：{}", arraydata);
			result = new JsonResult(arraydata, ResultCode.MSG_QUERY_SUCCESS,
					ResultCode.SUCCESS);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseArraySuccess(List<?> data, Integer resultCode,
			String MESSAGE) {
		logger.info("后端返回对象：{}", data);
			result = new JsonResult(data, MESSAGE, resultCode);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseFail() {
		result = new JsonResult(ResultCode.FAILED, ResultCode.MSG_ERROR);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}
	
	public String responseCreateFail() {
		result = new JsonResult(ResultCode.FAILED, ResultCode.MSG_CREATE_ERROR);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}
	
	public String responseUpdateFail() {
		result = new JsonResult(ResultCode.FAILED, ResultCode.MSG_UPDATE_ERROR);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}
	
	public String responseDeleteFail() {
		result = new JsonResult(ResultCode.FAILED, ResultCode.MSG_DELETE_FAILD);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseFail(String MESSAGE) {
			result = new JsonResult(MESSAGE, ResultCode.FAILED);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	public String responseFail(Integer resultCode, String MESSAGE) {
			result = new JsonResult(MESSAGE, resultCode);
		logger.info("后端返回数据：" + result.toJson());
		return result.toJson();
	}

	/**
	 * 11.29dzq 用于输入时间判断。
	 * 
	 * @return 返回时间是否合法
	 */
	public boolean timeJudge(Long startTime, Long endTime) {
		if (startTime == 0 && endTime == 0) {
			endTime = System.currentTimeMillis();
		}
		if (endTime == 0) {// 如果结束时间为0
			endTime = System.currentTimeMillis();// 取当前时间
		}
		if (startTime >= endTime || startTime < 0)
			return false;
		return true;
	}

}
